#include "TestTimeSeries.h"

#include "core/TimeSeries.h"
#include "core/SpectrumComponent.h"
#include "widgets/plotting/ScatterPlot.h"


#include <qwt_plot.h>

#include <QtCore/QFile>
#include <QtCore/QTextStream>

#include <iostream>

using namespace spectClean;

/**
 * @details
 */
void TestTimeSeries::test()
{
    QString filename = "time_series.txt";
    if (QFile::exists(filename))
        QFile::remove(filename);

    TimeSeries t;

    // Check the behaviour when the data file dosn't exist.
    {
        CPPUNIT_ASSERT_EQUAL(0, t.load(filename));
    }


    // Create a test time series file and load it.
    {
        QFile file(filename);
        if (!file.open(QIODevice::WriteOnly))
            CPPUNIT_FAIL("Failed to create test data file");
        QTextStream out(&file);
        unsigned num_samples = 10;
        for (unsigned i = 0; i < num_samples; ++i)
        {
            out << (double)i << " " << (double)i + 0.5 << endl;
        }
        file.close();

        CPPUNIT_ASSERT_EQUAL((int)num_samples, t.load(filename));
        CPPUNIT_ASSERT_EQUAL(num_samples, t.size());

        for (unsigned i = 0; i < num_samples; ++i)
        {
            CPPUNIT_ASSERT_EQUAL((double)i, t.time().at(i));
            CPPUNIT_ASSERT_EQUAL((double)i+0.5, t.amp().at(i));
        }
    }

    // Check clear and resize methods.
    {
        CPPUNIT_ASSERT(t.size() > 0);
        t.clear();
        CPPUNIT_ASSERT_EQUAL((unsigned)0, t.size());
        t.resize(20);
        CPPUNIT_ASSERT_EQUAL((unsigned)20, t.size());
        CPPUNIT_ASSERT_EQUAL((size_t)20, t.time().size());
        CPPUNIT_ASSERT_EQUAL((size_t)20, t.amp().size());
    }

    // Cleanup: Remove the test time series file.
    if (QFile::exists(filename))
        QFile::remove(filename);
}


void TestTimeSeries::test_generate()
{
    TimeSeries t;
    typedef SpectrumComponent fc;
    std::vector<fc> components;
    components.push_back(fc(1.0, 1.0));
    components.push_back(fc(0.1, 40.0));
    components.push_back(fc(0.3, 5.0));

    t.generateEven(500, 0.0, 2.0, components);

    components.clear();
    components.push_back(fc(1.0, 1.0));
    components.push_back(fc(0.5, 25.0));
    t.clear();
    t.generateRandom(100, 0.0, 2.0, components);

//    t.save("test_generated_even.txt");
//    t.save("test_generated_rand.txt");
}

